package net.wenku8.reader.view.file

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.LocalNavController
import net.wenku8.reader.enums.RouteFileRead
import net.wenku8.reader.state.StateReadFile
import net.wenku8.reader.ui.lite.LiteLine
import net.wenku8.reader.ui.lite.LiteLoading
import net.wenku8.reader.ui.lite.LitePanel
import net.wenku8.reader.ui.lite.LiteText
import net.wenku8.reader.ui.lite.LiteTextLink
import androidx.compose.runtime.collectAsState

@Composable
fun FileDetail() {
    val fileDetailViewModel = viewModel<FileDetailViewModel>()
    val navController = LocalNavController.current
    LaunchedEffect(Unit) {
        fileDetailViewModel.initData()
    }
    LitePanel(
        hostState = fileDetailViewModel.hostState,
        title = "小说详情",
        backNav = true
    ) {
        if (fileDetailViewModel.loaded){
            Column {
                FileBookPreview(fileDetailViewModel.bookFile, false)
                if (StateReadFile.bookFile.collectAsState().value.lastRead.title.trim().isNotEmpty()){
                    LiteLine(
                        onClick = {
                            fileDetailViewModel.toFileRead(fileDetailViewModel.bookFile.lastRead)
                            navController.navigate(RouteFileRead)
                        }
                    ){
                        LiteText("阅读历史：" + fileDetailViewModel.bookFile.lastRead.title)
                    }
                }
                LazyColumn{
                    items(items = fileDetailViewModel.chapterList) { chapter ->
                        LiteTextLink(chapter.title){
                            fileDetailViewModel.toFileRead(chapter)
                            navController.navigate(RouteFileRead)
                        }
                    }
                }
            }
        }else{
            LiteLoading("正在加载章节信息...")
        }
    }
}

